package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 保存套餐数据
     * 1.创建setmeal对象
     * 2.将setmealDTO拷贝到setmeal对象中
     * 3.将setmeal插入到表
     * 4.从setmealDTO中获取dishIds集合
     * 5.遍历dishIds集合，将每个dishId设置到setmealDish对象中
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);
        //获取生成的套餐id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
}

    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        /**
         * 1.在setmeal中查询套餐信息
         * 2.在setmeal_dish中查询套餐对应的菜品信息
         * 3.将setmal中套餐信息和菜品信息封装到SetmealVO中并返回
         */
        Setmeal setmeal=setmealMapper.findById(id);
        List<SetmealDish> setmealDishes=setmealMapper.getByIdWithDish(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO); // 拷贝套餐属性
        setmealVO.setSetmealDishes(setmealDishes);   // 设置菜品列表
        return setmealVO;
    }

    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);

    }

    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        /**
         * 1.修改Setmeal表(SetmealDTO->Setmeal)
         * 2.删除Setmeal_dish表
         * 3.批量插入Setmeal_dish表
         */
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteByDishId(setmealDTO.getId());
        List<SetmealDish> flavors=setmealDTO.getSetmealDishes();
        flavors.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    @Override
    public void deleteBatch(Long[] ids) {
        /**
         * 删除套餐
         * 1.遍历获取套餐id
         * 2.判断当前套餐是否在售，在售不能删除
         * 3.删除套餐表中数据
         */
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == 1) {
                //当前套餐正在启售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            //删除套餐表中数据
            setmealMapper.deleteById(id);
            //删除套餐菜品关系表中数据
            setmealDishMapper.deleteByDishId(id);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal=Setmeal.builder().
                status(status).
                id(id).
                build();
        setmealMapper.update(setmeal);
    }


}
